package com.training1.freemarker.directive;

import java.io.IOException;
import java.util.Map;

import com.training1.core.common.enums.EnumUtils;
import com.training1.core.common.utils.StringUtils;

import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 
 * @author hl
 */
public class EnumDirective implements TemplateDirectiveModel{

	@SuppressWarnings("rawtypes")
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		String enumName = ((SimpleScalar)(params.get("enum"))).getAsString();
		Integer value = Integer.parseInt(params.get("value").toString());
		if(StringUtils.anyIsEmpty(enumName,value)){
			return ;
		}
		env.getOut().write(EnumUtils.getEnumLabel(enumName, value));
	}

}
